//
//  BKHeZhaoStep3ViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/4.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHeZhaoStep3ViewController.h"
#import "BKTextView.h"
#import "BKTextField.h"
#import "BKHeZhaoViewModel.h"
#import <MBProgressHUD.h>
#import "UIImage+BKScale.h"

@interface BKHeZhaoStep3ViewController ()<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>


@property (weak, nonatomic) IBOutlet BKTextField *name;
@property (weak, nonatomic) IBOutlet BKTextField *xuehao;
@property (weak, nonatomic) IBOutlet BKTextView *liuyan;
@property (weak, nonatomic) IBOutlet UIButton *touxiang;


@end

@implementation BKHeZhaoStep3ViewController
- (IBAction)p_back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _params  = [NSMutableDictionary dictionary];

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _shenqing.hidden = _isSQHiden;
    _tijiao.hidden = _isTJHiden;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)openMenu
{
    //在这里呼出下方菜单按钮项
    UIActionSheet *myActionSheet = [[UIActionSheet alloc]
                     initWithTitle:nil
                     delegate:self
                     cancelButtonTitle:@"取消"
                     destructiveButtonTitle:nil
                     otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
    
    [myActionSheet showInView:self.view];
    
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    
    switch (buttonIndex)
    {
        case 0:  //打开照相机拍照
            [self takePhoto];
            break;
            
        case 1:  //打开本地相册
            [self LocalPhoto];
            break;
    }
}

//开始拍照
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
       
        [self presentViewController:picker animated:YES completion:nil];
        
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

//打开本地相册
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
    
}

//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* originalImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        
        UIGraphicsBeginImageContext(CGSizeMake(120, 120));
        [originalImage drawInRect:CGRectMake(0, 0, 120, 120)];
        
        
        UIImage *image =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        
        _touxiang.imageView.image = image;
       
        
        NSData *data = UIImageJPEGRepresentation(image, .75f);
        
        NSString *encodedImageStr = [data base64EncodedStringWithOptions:0];
        NSString *string = [@"data:image/jpeg;base64," stringByAppendingString:encodedImageStr];
        
        
        
        
        _params[@"imgstr"]= [encodedImageStr stringByReplacingOccurrencesOfString:@"+" withString:@"%2b" options:0 range:NSMakeRange(0, [encodedImageStr length])];
        
        
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
   
    [picker dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)p_submit:(id)sender {
    
    
    if (_params[@"imgstr"] == nil) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先选取头像" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    if ([_xuehao.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入学校" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    

    if ([_name.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入下姓名" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if ([_liuyan.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入留言" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    _params[@"xuehao"] = _xuehao.text;
    _params[@"name"] = _name.text;
    _params[@"liuyan"] = _liuyan.text;
    _params[@"p"] = @"13";
    _params[@"id"] = _ban;
    _params[@"type"] = @"jpg";
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.labelText = @"正在提交";
    hud.mode = MBProgressHUDModeIndeterminate;
    [hud show:YES];
    
    [BKHeZhaoViewModel shengqingWithParams:_params
                               success:^{
                                   [hud hide:YES];
                                   dispatch_queue_t mainQueue = dispatch_get_main_queue();
                                   dispatch_async(mainQueue, ^(void) {
                                       
                                   });
                               } failure:^(NSString *message) {
                                   
                                   dispatch_queue_t mainQueue = dispatch_get_main_queue();
                                   dispatch_async(mainQueue, ^(void) {
                                       [hud hide:YES];
                                       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                                       [alertView show];
                                       
                                   });
                               }];



    
}
- (IBAction)tijiao:(id)sender {
    
    if (_params[@"imgstr"] == nil) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先选取头像" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    if ([_xuehao.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入学校" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    
    if ([_name.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入下姓名" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if ([_liuyan.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入留言" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    _params[@"xuehao"] = _xuehao.text;
    _params[@"name"] = _name.text;
    _params[@"liuyan"] = _liuyan.text;
    _params[@"p"] = @"13";
    _params[@"id"] = _ban;
    _params[@"type"] = @"jpg";
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.labelText = @"正在提交";
    hud.mode = MBProgressHUDModeIndeterminate;
    [hud show:YES];
    
    [BKHeZhaoViewModel tijaoWithParams:_params
                               success:^{
        [hud hide:YES];
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^(void) {
        
        });
    } failure:^(NSString *message) {
        
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
        dispatch_async(mainQueue, ^(void) {
            [hud hide:YES];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alertView show];
            
        });
    }];
    

}

@end
